// CREATE TABLE `t_message` (
//     `id` int(11) NOT NULL AUTO_INCREMENT,
//     `mid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
//     `last_mid` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '被回复的消息id',
//     `fuid` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
//     `tuid` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
//     `content_type` int(255) DEFAULT NULL COMMENT '0 txt 1html',
//     `content` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
//     `has_read` int(255) DEFAULT '0',
//     `password` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
//     `create_date` datetime DEFAULT NULL,
//     PRIMARY KEY (`id`,`mid`)
//   ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci


let { v4: uuidv4 } = require("uuid")
let knex = require("../dao")
let TNAME = 't_message'
class Message {
    Add = (data, callback) => {
        knex(TNAME).insert(data).then(res => {
            callback(undefined, { code: 200, msg: "添加成功", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Update = (data, callback) => {
        let { mid } = data
        knex(TNAME).where({ mid }).update(data).then(res => {
            callback(undefined, { code: 200, msg: "更新成功", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    List = ({ page, page_size }, callback) => {
        knex(TNAME).select().offset(page_size || 10).limit(page || 0).then(res => {
            callback(undefined, { code: 200, msg: "", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Detail = ({ mid }, callback) => {
        knex(TNAME).select().where({ mid }).then(res => {
            callback(undefined, { code: 200, msg: "", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Remove = ({ mid }, callback) => {
        knex(TNAME).where({ mid }).del().then(res => {
            callback(undefined, { code: 200, msg: "删除成功", data: res })

        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: res })

        })
    }
}

module.exports = Message